/*
 * Copyright (C) 2004, Velocityme Partnership
 * MenuButtonAction.java
 *
 * Created on 06 September 2004, 08:34
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.Node;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.session.SearchLocal;
import com.velocityme.session.TaskLocal;
import com.velocityme.session.TreeLocal;
import com.velocityme.session.UserLocal;
import com.velocityme.www.actionforms.ButtonActionForm;
import com.velocityme.www.actionforms.NodeIdActionForm;
import com.velocityme.www.utility.NodeUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class MenuButtonAction extends org.apache.struts.action.Action {
    
    /** Creates a new instance of MenuButtonAction */
    public MenuButtonAction() {
    }
    
    @Override
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
    
        ButtonActionForm buttonAF = (ButtonActionForm)form;
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("buttonValues", null);
        if (buttonAF.getButton().equals("file-home")) {
            return mapping.findForward("ACTION_FILE_HOME_SUCCESS");
        }
        if (buttonAF.getButton().equals("file-logout")) {
            // make sure we don't jump straight back to this node on login
            session.removeAttribute("nodeId");
            if (key != null) {
                key.logout();
//                key.remove();
            }
            session.invalidate();
            return mapping.findForward("ACTION_FILE_LOGOUT_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-all-responsible")) {
            session.setAttribute("resultValues", lookupSearchBean().findAllMyResponsibleTasks(key));
            return mapping.findForward("ACTION_ALL_MY_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-all-interested")) {
            session.setAttribute("resultValues", lookupSearchBean().findAllMyInterestedTasks(key));
            return mapping.findForward("ACTION_ALL_MY_INTERESTED_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().startsWith("bookmark-nodeId-")) {
            Long bookmarkNodeId = Long.parseLong(buttonAF.getButton().substring(16));
            request.setAttribute("nodeId", bookmarkNodeId);
            return mapping.findForward("ACTION_BOOKMARK_SHOW");
        }
        if (buttonAF.getButton().equals("help-content")) {
            return mapping.findForward("ACTION_HELP_CONTENT_SUCCESS");
        }
/*        if (buttonAF.getButton().equals("help-about")) {
            session.setAttribute("version", key.getServerImplementationVersion());
            return mapping.findForward("ACTION_HELP_ABOUT_SUCCESS");
        }*/
        Long nodeId = (Long)session.getAttribute("nodeId");
        Node node = null;
        if (nodeId != null && nodeId.longValue() != 0L) {
            request.setAttribute("nodeId", nodeId);
            node = lookupNodeBean().findNode(key, nodeId);
            session.setAttribute("node", node);
        }
        if (buttonAF.getButton().equals("bookmark-show") || buttonAF.getButton().equals("bookmark-hide")) {
            lookupUserBean().showBookmarks(key, buttonAF.getButton().equals("bookmark-show"));
            if (nodeId != null && nodeId.longValue() != 0L)
                return mapping.findForward("ACTION_SUCCESS");
            else
                return mapping.findForward("ACTION_FILE_HOME_SUCCESS");
        }
        if (buttonAF.getButton().equals("edit-node-mark")) {
            session.setAttribute("markedNodeId", nodeId);
            return mapping.findForward("ACTION_MARK_SUCCESS");
        }
        if (buttonAF.getButton().equals("edit-node-move")) {
            Long childId = (Long)session.getAttribute("markedNodeId");
            node = lookupNodeBean().findNode(key, nodeId);
            Node childNode = lookupNodeBean().findNode(key, childId);
            lookupTreeBean().moveChildNode(key, node, childNode);
            session.removeAttribute("markedNodeId");
            return mapping.findForward("ACTION_MOVE_SUCCESS");
        }
        if (buttonAF.getButton().equals("edit-node-delete")) {
            NodeIdActionForm nodeAF = new NodeIdActionForm();
            nodeAF.setNodeId(nodeId);
            session.setAttribute("nodeAF", nodeAF);
            return mapping.findForward("ACTION_DELETE_CHECK");
        }
        if (buttonAF.getButton().equals("edit-node-link")) {
            Long childId = (Long)session.getAttribute("markedNodeId");
            lookupTreeBean().createNodeLink(key, nodeId, childId);
            session.removeAttribute("markedNodeId");
            return mapping.findForward("ACTION_LINK_SUCCESS");
        }
        if (buttonAF.getButton().equals("edit-node-dependency")) {
            Long dependencyId = (Long)session.getAttribute("markedNodeId");
            lookupTaskBean().createDependency(key, dependencyId, nodeId);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_DEPENDENCY_SUCCESS");
        }
        if (buttonAF.getButton().equals("edit-node-dependant")) {
            Long dependencyId = (Long)session.getAttribute("markedNodeId");
            lookupTaskBean().createDependency(key, nodeId, dependencyId);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_DEPENDANT_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-node")) {
            return mapping.findForward("ACTION_CREATE_NODE_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-sprint")) {
            return mapping.findForward("ACTION_CREATE_SPRINT_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-project")) {
            return mapping.findForward("ACTION_CREATE_PROJECT_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-anchor")) {
            return mapping.findForward("ACTION_CREATE_ANCHOR_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-action")) {
            return mapping.findForward("ACTION_CREATE_ACTION_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-issue")) {
            return mapping.findForward("ACTION_CREATE_ISSUE_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-organisation")) {
            return mapping.findForward("ACTION_CREATE_ORGANISATION_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-department")) {
            return mapping.findForward("ACTION_CREATE_DEPARTMENT_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-person")) {
            return mapping.findForward("ACTION_CREATE_PERSON_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-user")) {
            return mapping.findForward("ACTION_CREATE_USER_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-group")) {
            return mapping.findForward("ACTION_CREATE_GROUP_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-state")) {
            return mapping.findForward("ACTION_CREATE_STATE_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-status")) {
            return mapping.findForward("ACTION_CREATE_STATUS_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-task-type")) {
            return mapping.findForward("ACTION_CREATE_TASKSTATEMACHINE_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-role")) {
            return mapping.findForward("ACTION_CREATE_ROLE_SUCCESS");
        }
        if (buttonAF.getButton().equals("create-user-access")) {
            session.setAttribute("nodePathName", node.getPathName());
            session.setAttribute("userPairs", NodeUtil.getAllUserPairs(key));
            session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key));
            return mapping.findForward("ACTION_CREATE_USER_ACCESS_SUCCESS");
        }            
        if (buttonAF.getButton().equals("create-group-access")) {
            session.setAttribute("nodePathName", node.getPathName());
            session.setAttribute("groupPairs", NodeUtil.getAllGroupPairs(key));
            session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key));
            return mapping.findForward("ACTION_CREATE_GROUP_ACCESS_SUCCESS");
        }            
        if (buttonAF.getButton().equals("search-tasks-my-responsible")) {
            session.setAttribute("nodeValue", node);
            session.setAttribute("resultValues", lookupSearchBean().findMyResponsibleTasks(key, node));
            return mapping.findForward("ACTION_MY_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-my-interested")) {
            session.setAttribute("nodeValue", node);
            session.setAttribute("resultValues", lookupSearchBean().findMyInterestedTasks(key, node));
            return mapping.findForward("ACTION_MY_INTERESTED_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-others-responsible")) {
            return mapping.findForward("ACTION_OTHERS_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-new")) {
            return mapping.findForward("ACTION_NEW_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-active")) {
            session.setAttribute("nodeValue", node);
            session.setAttribute("resultValues", lookupSearchBean().findActiveTasks(key, node));
            return mapping.findForward("ACTION_ACTIVE_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-completed")) {
            return mapping.findForward("ACTION_COMPLETED_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-changed")) {
            return mapping.findForward("ACTION_CHANGED_TASKS_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-tasks-advanced")) {
            return mapping.findForward("ACTION_ADVANCED_SEARCH_SUCCESS");
        }
        if (buttonAF.getButton().equals("search-time")) {
            return mapping.findForward("ACTION_TIME_REPORT_SUCCESS");
        }
        if (buttonAF.getButton().equals("bookmark-add-node")) {
            return mapping.findForward("ACTION_BOOKMARK_ADD_NODE");
        }
        if (buttonAF.getButton().equals("bookmark-remove-node")) {
            return mapping.findForward("ACTION_BOOKMARK_REMOVE_NODE");
        }
        return mapping.findForward("ACTION_FAILED");
    }

    private SearchLocal lookupSearchBean() {
        try {
            Context c = new InitialContext();
            return (SearchLocal) c.lookup("java:comp/env/SearchBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private UserLocal lookupUserBean() {
        try {
            Context c = new InitialContext();
            return (UserLocal) c.lookup("java:comp/env/UserBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private TreeLocal lookupTreeBean() {
        try {
            Context c = new InitialContext();
            return (TreeLocal) c.lookup("java:comp/env/TreeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private TaskLocal lookupTaskBean() {
        try {
            Context c = new InitialContext();
            return (TaskLocal) c.lookup("java:comp/env/TaskBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
